<?php
namespace app\proxy\controller;
use think\Db;
use app\service\TechnService;
class User extends Base
{
    public function _initialize()
    {
        // 调用父类前置方法
        parent::_initialize();

        // 登录校验
        $this->IsLogin();
    }

    /**
     * 服务列表
     */
    public function index()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        if(request()->isPost())
        {
            $params = input();
            if(!empty($params['page']) && !empty($params['limit']))
            {
                $map = [];


                if(!empty($params['nickname']))
                {
                    $map['nickname'] = ['like','%'.$params['nickname'].'%'];
                }

                if(!empty($params['mobile']))
                {
                    $map['mobile'] = $params['mobile'];
                }
                // print_r($map);exit;

                $count = Db::name('user')->where($map)->count();
                $list_data = Db::name('user')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
                if(!empty($list_data))
                {
                    //遍历处理
                    foreach($list_data as $key => $value)
                    {
                        $list_data[$key]['save_url'] = url('user/saveinfo', ['id' => $value['id']]);
                        if(!empty($value['beizhu'])){
                            $list_data[$key]['nickname']=$value['nickname'].'('.$value['beizhu'].')';
                        }
                    }
                    $result['code'] = 0;
                    $result['msg'] = "请求成功";
                    $result['data'] = $list_data;
                    $result['count'] = $count;
                }else
                {
                    $result['code'] = -1;
                    $result['msg'] = "暂无数据";
                }
            }
            echo json_encode($result);
        }else
        {
            return $this->fetch();
        }
    }

    public function saveinfo()
    {
        // 参数
        $params = input();
        $info = [];

        //查询
        $info = Db::name('user')->where(['id' => $params['id']])->find();

        $this->assign('info',$info);

        return $this->fetch();
    }

    /**
     * 服务添加/修改
     */
    public function save()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        if(request()->isPost())
        {
            $params = input();

            $params['upd_time'] = time();


            Db::name('user')->where(['id'=>intval($params['id'])])->update($params);
            //是否成功
            $result['code'] = 0;

            $result['msg'] = "操作成功";

        }
        echo json_encode($result);
    }

}